package ${package.Controller};


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import com.cg.common.core.domain.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.annotation.Resource;

<#if restControllerStyle>
<#else>
    import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
    import ${superControllerClassPackage};
</#if>


import java.util.List;

/**
* <p>
    * ${table.comment} 前端控制器
    * </p>
*
* @author lzw
*/
@Api(tags = "${table.comment}")
<#if restControllerStyle>
    @RestController
<#else>
    @Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
    public class ${table.controllerName} extends ${superControllerClass} {
<#else>
    public class ${table.controllerName} {
</#if>

private static final String ENTITY_NAME = "${entity}";

@Resource
private ${table.serviceName} ${table.serviceName?uncap_first};

@ApiOperation(value = "${table.comment}分页列表", response = ${entity}.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "pageCurrent", value = "页面", dataType = "Long"),
@ApiImplicitParam(name = "pageSize", value = "页面数据量", dataType = "Long"),
@ApiImplicitParam(name = "sort", value = "排序方式排序[true:正序; false:倒序]", dataType = "Boolean"),
@ApiImplicitParam(name = "sortName", value = "排序字段,参照返回字段", dataType = "String")})
@PostMapping(value = "/page")
public  R
<IPage<${entity}>> list( @RequestBody ${entity} param,
@RequestParam("pageCurrent") Integer pageCurrent,
@RequestParam("pageSize") Integer pageSize,
@RequestParam(value = "sort" , required = false , defaultValue = "false" ) Boolean sort,
@RequestParam(value = "sortName" , required = false ) String sortName) {
IPage<${entity}> page = ${table.serviceName?uncap_first}.page(param,pageCurrent,pageSize,sort,sortName);
return R.ok(page);
}

@ApiOperation(value = "${table.comment}详情", response = ${entity}.class)
@GetMapping(value = "/info/{id}")
public  R<${entity}> info(@PathVariable Long id) {
${entity} data = ${table.serviceName?uncap_first}.info(id);
return  R.ok(data);
}

@ApiOperation(value = "${table.comment}新增")
@PostMapping(value = "/add")
public  R
<Void> add(@RequestBody ${entity} param) {
    ${table.serviceName?uncap_first}.add(param);
    return R.ok(null,"新增成功");
    }

    @ApiOperation(value = "${table.comment}新增(批量新增)")
    @PostMapping(value = "/adds")
    public R
    <Void> adds(@RequestBody List<${entity}> param) {
        ${table.serviceName?uncap_first}.adds(param);
        return R.ok(null,"新增成功");
        }

        @ApiOperation(value = "${table.comment}修改(单个修改)")
        @PostMapping(value = "/modify")
        public R
        <Void> modify(@RequestBody ${entity} param) {
            ${table.serviceName?uncap_first}.modify(param);
            return R.ok(null,"修改成功");
            }

            @ApiOperation(value = "${table.comment}修改(批量修改)")
            @PostMapping(value = "/modifys")
            public R
            <Void> modifys(@RequestBody List<${entity}> param) {
                ${table.serviceName?uncap_first}.modifys(param);
                return R.ok(null,"批量修改成功");
                }

                @ApiOperation(value = "${table.comment}删除(单个条目)")
                @GetMapping(value = "/remove/{id}")
                public R
                <Void> remove(@PathVariable Long id) {
                    ${table.serviceName?uncap_first}.remove(id);
                    return R.ok(null,"删除成功");
                    }

                    @ApiOperation(value = "${table.comment}删除(多个条目)")
                    @PostMapping(value = "/removes")
                    public R
                    <Void> removes(@RequestBody List
                        <Long> ids) {
                            ${table.serviceName?uncap_first}.removes(ids);
                            return R.ok(null,"批量删除成功");
                            }

                            @ApiOperation(value = "${table.comment}导出Excel")
                            @RequestMapping("/export")
                            public R
                            <Void> download(HttpServletRequest request, HttpServletResponse response) throws Exception {
                                response.setHeader("content-Type", "application/vnd.ms-excel");
                                response.setHeader("Content-Disposition", "attachment;filename=user.xls");
                                List<${entity}> list = ${table.serviceName?uncap_first}.list();
                                Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ${entity}.class,
                                list);
                                workbook.write(response.getOutputStream());
                                return R.ok(null,"导出Excel成功");
                                }

                                @ApiOperation(value = "${table.comment}导入Excel")
                                @RequestMapping("/importData")
                                public R
                                <List
                                <${entity}>> importData(@RequestParam("file") MultipartFile multipartFile) throws
                                Exception {
                                ImportParams params = new ImportParams();
                                params.setHeadRows(1);
                                params.setTitleRows(0);
                                List<${entity}> result =
                                ExcelImportUtil.importExcel(multipartFile.getInputStream(), ${entity}.class, params);
                                return R.ok(result,"导出Excel成功");
                                }
                                @ApiImplicitParams({
                                @ApiImplicitParam(name = "txtStatus", value = "文本是否换行，true-换行，false-不换行",
                                dataType = "Boolean"),
                                @ApiImplicitParam(name = "key", value = "换行依据", dataType = "String"),
                                @ApiImplicitParam(name = "textContent", value = "内容", dataType = "String")
                                })
                                @GetMapping("/exportTxt")
                                @ApiOperation(value = "导出text", notes = "导出text")
                                public void exportLog(HttpServletResponse response,@RequestParam("txtStatus") Boolean
                                txtStatus,@RequestParam("key")String key,@RequestParam("textContent")String textContent)
                                throws UnsupportedEncodingException {
                                StringBuffer content = new StringBuffer();
                                if(txtStatus){
                                String[] split = textContent.split(key);
                                for (String s : split) {
                                content.append(s);
                                //换行字符
                                content.append("\r\n");
                                }
                                }else{
                                content.append(textContent);
                                }
                                response.setCharacterEncoding("utf-8");
                                //设置响应的内容类型
                                response.setContentType("text/plain");
                                //设置文件的名称和格式
                                response.setHeader("Content-Disposition", "attachment;filename=" +
                                URLEncoder.encode("error.txt", "utf-8"));
                                BufferedOutputStream buff = null;
                                ServletOutputStream outStr = null;
                                try {
                                outStr = response.getOutputStream();
                                buff = new BufferedOutputStream(outStr);
                                buff.write(content.toString().getBytes(StandardCharsets.UTF_8));
                                buff.flush();
                                buff.close();
                                } catch (Exception e) {
                                e.printStackTrace();
                                }finally {
                                try {
                                buff.close();
                                outStr.close();
                                } catch (Exception e) {
                                e.printStackTrace();
                                }
                                }
                                }
                                }
                                </#if>
